package org.eleorc.ioc.beans.definition.inheritance.ext;

import org.eleorc.ioc.beans.definition.inheritance.DerivedTestBean;
import org.eleorc.utils.ApplicationContextUtils;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.context.support.GenericApplicationContext;

import java.util.List;

/**
 *  非xml方式创建 BeanDefiniton
 *  并设置BeanDefinition的 父子 关系
 */
public class GenericBeanDefinitionDemo {
    public static void main(String[] args) {

        AbstractBeanDefinition parentBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition()
                .addPropertyValue("name", "parent")
                .addPropertyValue("age", 18)
                .setAbstract(true)
                .getBeanDefinition();

        AbstractBeanDefinition childBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(DerivedTestBean.class)
                .setParentName("inheritedTestBean")
                .addPropertyValue("name", "child")
                .getBeanDefinition();

        // register
        GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
        genericApplicationContext.registerBeanDefinition("inheritedTestBean",parentBeanDefinition);
        genericApplicationContext.registerBeanDefinition("inheritsWithDifferentClass",childBeanDefinition);

        ApplicationContextUtils.printBeanDefinitionNames(genericApplicationContext);

        List<BeanDefinition> beanDefinitions = ApplicationContextUtils.getBeanDefinitions(genericApplicationContext.getBeanFactory());
        beanDefinitions.forEach(System.out::println);

        ApplicationContextUtils.printBeanDefinitionInfo(genericApplicationContext.getBeanFactory());

        genericApplicationContext.refresh();

        DerivedTestBean bean = genericApplicationContext.getBean(DerivedTestBean.class);
        System.out.println(bean);


    }
}
